﻿using Adp;
using Microsoft.AspNetCore.Mvc;
using Yqd.Models;
using Yqd.Controllers.Filter;
using Adp.Common;
using System.Linq;
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Yqd.Core;

namespace Yqd.Controllers
{
    [Area("Yqd")]
    public class WxExactAssignController : Controller
    {
        YqdContext ctx;

        public WxExactAssignController(YqdContext ctx)
        {
            this.ctx = ctx;
        }

        [User]
        public ActionResult GetUnit()
        {
            var TaskID = Form.GetLong("TaskID");
            var uIDs = ctx.Yqd_Assign.Where(it => it.TaskID == TaskID && it.ItemType == Enm.事项类型.精准督查).Select(it => it.LeadUnit).ToArray();
            var r = ctx.Yqd_Unit.Where(it => uIDs.Contains(it.UnitCode));
            return this.ToJson(new
            {
                success = true,
                data = r.Distinct().Select(it => new
                {
                    Value = it.UnitCode,
                    Text = it.Name
                })
            });
        }

        [User]
        public ActionResult GetForm()
        {
            var TaskID = Form.GetLong("TaskID");
            var UnitCode = Form.GetString("UnitCode");
            var TaskName = "";
            var task = ctx.Yqd_Task.FirstOrDefault(it => it.TaskID == TaskID);
            if (task != null) TaskName = task.TaskName;
            var assign = ctx.Yqd_Assign.Where(it => it.ItemType == Enm.事项类型.精准督查 && it.TaskID == TaskID && it.LeadUnit == UnitCode).Select(it => new { it.ItemID, it.Item, it.LeadUnit, it.AssignLimit, it.EmployeeCode,it.UnitCode }).ToList();
            var sIDs = assign.Select(it => it.ItemID).ToArray();
            var supervise = ctx.Yqd_ExactSupervise.Where(it => it.TaskID == TaskID && sIDs.Contains(it.SuperviseID));
            var user = ctx.Yqd_User.Select(it => new { it.EmployeeCode, it.Name });
            var unit = ctx.Yqd_Unit.Select(it => new { it.UnitCode, it.Name });
            var data = new List<object>();
            string Assigner = "", AssignUnit = "", AssignLimit = "";
            foreach (var s in supervise)
            {
                var a = assign.FirstOrDefault(n => n.ItemID == s.SuperviseID);
                if (a != null) {
                    if (Assigner == "")
                    {
                        AssignLimit = a.AssignLimit.ToString("yyyy年MM月dd日");
                        Assigner = user.FirstOrDefault(m => m.EmployeeCode == a.EmployeeCode)?.Name;
                        AssignUnit = unit.FirstOrDefault(m => m.UnitCode == a.UnitCode)?.Name;
                    }
                    data.Add(new
                    {
                        s.Problem,
                        Condition = a.Item,
                        Photos = !string.IsNullOrWhiteSpace(s.Photos) ? JArray.Parse(s.Photos) : new JArray()
                    });
                }
            }
            return this.ToJson(new
            {
                success = true,
                FormID = SuperviseService.GetSuperviseNo(TaskID),
                TaskName,
                LeadUnit = unit.FirstOrDefault(it => it.UnitCode == UnitCode)?.Name,
                AssignLimit,
                Assigner,
                AssignUnit,
                data
            });
        }
    }
}
